home *** CD-ROM | disk | FTP | other *** search
- #include "SavePicture.h"
-
- /*
- This (handy?) little 'ol function saves a picture as a standard "PICT"
- file. Note that barely any error-checking is done; you should add more
- extensive checking for your own application(s). As an excuse for error
- checking, this routine beeps when it encounters one and exits (quite
- ungracefully, too).
-
- A special note concerning PICT files: A pict file is like any other
- "data" file in that you write stuff to the data fork. However, the
- pict file also contains a "special" 512-byte header. Most of the time
- you can ignore this. So when writing to or reading from a pict file,
- just skip the first 512 bytes (i.e. SetFPos(fileRefNum, fsFromStart, 512))
- and all will be well. My experience is that almost all applications
- ignore this 512-byte header (but keep it due to ancient history).
-
- By the way, the code herein is System 7 dependent. If you wish to
- change to System 6 compatibility, you'll have to change the
- StandardFileReply to an SFReply, etc. etc. etc.
- (But why System 6?)
-
-
- Version History:
- 1.1 Oct 31 95
- Added creator argument, handling of "replace file?" situations.
- 1.2 Nov 2 95
- Separated save picture into 2 routines (one with prompt, the
- other doing the actual save) and added SavePictureResource.
- Now a complete suite of PICT saving routines!
- */
-
- Boolean SavePicture(
- Handle thePic,
- OSType creator,
- Str31 fileName) {
-
- StandardFileReply reply;
- OSErr myErr;
-
- // Put up the standard save dialog
- StandardPutFile("\pSave picture into:", fileName, &reply);
-
- if (reply.sfGood) {
- if (reply.sfReplacing) {
- myErr = FSpDelete(&reply.sfFile);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
- }
-
- // Create the file.
- myErr = FSpCreate(&reply.sfFile, creator, 'PICT', reply.sfScript);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- return(SavePictureFile(&reply.sfFile, thePic));
- }
- else
- return(false);
- } // END SavePicture
-
- // ---------------------------------------------------------------------------
-
- Boolean SavePictureFile(
- FSSpec *pictFile,
- Handle thePic) {
-
- OSErr myErr;
- short fileRefNum;
- long picSize; // The size of our picture, give or take a header
-
-
- // Open it up (the data fork, that is)...
- myErr = FSpOpenDF(pictFile, fsCurPerm, &fileRefNum);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- // Lock up the picture, since it may move when writing to
- // our file. Also get the size of our picture. Then tell
- // the File Manager to allocate enough space in the file
- // for us, keeping in mind the 512-byte header...
- HLock(thePic);
- picSize = GetHandleSize(thePic);
- picSize = picSize + 512;
- myErr = SetEOF(fileRefNum, picSize);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- // Alright, time to do the dirty deed (but first,
- // since we don't want to write in the header, tell
- // the File Manager to skip the first 512-bytes)
- myErr = SetFPos(fileRefNum, fsFromStart, 512);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- // OK, ok, here we are. Write to the file.
- // Note: FSWrite expects a pointer to our data (i.e. the
- // picture). So we'll have to dereference the
- // picture handle.
- picSize = GetHandleSize(thePic);
- myErr = FSWrite(fileRefNum, &picSize, (Ptr)*thePic);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- // Close it up. You may have to call FlushVol() or
- // such to tell the File Manager to write the data
- // to the file immediately (otherwise it waits a
- // while...)
- myErr = FSClose(fileRefNum);
- if (myErr != noErr) {
- SysBeep(10);
- return(false);
- }
-
- return(true);
- } // END SavePictureFile
-
- // ---------------------------------------------------------------------------
-
- Boolean SavePictureResource(
- PicHandle thePic,
- short fileRefNum,
- short resID,
- Str31 resName) {
-
- Handle picHdl;
- short oldFileRefNum;
-
- oldFileRefNum = CurResFile();
- UseResFile(fileRefNum);
-
- // Make sure resource doesn't already exist (if it does, delete it)
- do {
- picHdl = Get1Resource('PICT', resID);
- if (picHdl != NULL) {
- RemoveResource((Handle)picHdl);
- DisposeHandle((Handle)picHdl);
- }
- } while (picHdl != NULL);
-
- if (resName != NULL)
- AddResource((Handle)thePic, 'PICT', resID, resName);
- else
- AddResource((Handle)thePic, 'PICT', resID, "\p");
- if (ResError() != noErr) {
- UseResFile(oldFileRefNum);
- return(false);
- }
-
- WriteResource((Handle)thePic);
- if (ResError() != noErr) {
- UseResFile(oldFileRefNum);
- return(false);
- }
- DetachResource((Handle)thePic);
- if (ResError() != noErr) {
- UseResFile(oldFileRefNum);
- return(false);
- }
-
- UseResFile(oldFileRefNum);
- return(true);
- } // END SavePictureResource